package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.auth.entity.custom.SysOptLogCustom;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.entity.pbsys.SysOperationLogWithBLOBs;
import com.pb.infra.auth.mapper.custom.SysOperationLogCustomMapper;
import com.pb.infra.auth.mapper.pbsys.SysOperationLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统操作日志管理
 *
 * @author liudebao
 * @date 2020/1/5
 */
@Service
public class SysOptLogService {

    /**
     * 日志单表DAO映射
     */
    @Resource
    private SysOperationLogMapper logMapper;

    /**
     * 日志多表DAO映射
     */
    @Resource
    private SysOperationLogCustomMapper logCustomMapper;

    /**
     * 新增操作日志
     *
     * @param log
     * @return
     */
    @Transactional
    public void insert(SysOperationLogWithBLOBs log) {
        logMapper.insertSelective(log);
    }

    /**
     * 查询日志列表
     *
     * @param log
     * @return
     */
    public Result select(Map<String, Object> log, Page page) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", log.get("userId"));
        map.put("tableName", log.get("tableName"));
        map.put("actionType", log.get("actionType"));
        List<SysOptLogCustom> list = logCustomMapper.getOptLogData(map);
        /*if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (!StringUtil.isEmpty(list.get(i).get("newJson"))) {
                    //String str = new String((byte[]) list.get(i).get("newJson"), StandardCharsets.UTF_8);
                    String str = getClobData(list.get(i).get("newJson"));
                    list.get(i).put("newJson", str);
                }
                if (!StringUtil.isEmpty(list.get(i).get("oldJson"))) {
                    //String str = new String((byte[]) list.get(i).get("oldJson"), StandardCharsets.UTF_8);
                    String str = getClobData(list.get(i).get("oldJson"));
                    list.get(i).put("oldJson", str);
                }
            }
        }*/
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    /**
     *Clob数据转换
     * @param json
     * @return
     */
    public String getClobData (Object json) {
        Reader is =null;
        BufferedReader br = null;
        try {
            Clob clob = (Clob) json;
            is = clob.getCharacterStream();// 得到流
            br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s);
                s = br.readLine();
            }
            String reString = sb.toString();
            return reString;
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }finally {
            try {
                is.close();
                br.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    /**
     *  获取日志序号
     * @return
     */
    public int selLogId() {
        return  logCustomMapper.selLogId();
    }

    /**
     * 查询操作类型
     * @return
     */
    public Result selectType() {
        return Result.ok(logCustomMapper.selectType());
    }
}
